package org.example.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;


/**
 * Combiner 合并案例
 *
 */
public class WordCountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1、获取JOB
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        //2、设置jar包路径
        job.setJarByClass(WordCountDriver.class);

        //3、关联mapper 和 reducer (让他俩怎么产生联系)
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);

        //4、设置map输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //5、设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // TODO 设置自定义combiner 的关联
//        job.setCombinerClass(WordCountCombiner.class);

//        job.setNumReduceTasks(0);

        // TODO 不影响业务逻辑可以直接使用WordCountReducer、明显看出 输入输出的体积变小
        job.setCombinerClass(WordCountReducer.class);

        //6、设置输入路径和输出路径
        FileInputFormat.setInputPaths(job, new Path("D:\\input\\inputword1"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\hadoop\\output1"));

        //7、提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }
}
